import cv2
import mediapipe as mp
import math
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
image = cv2.imread("right-frontal.jpg")
#image = cv2.imread("hand.jpg")
#image = cv2.imread("front-back.jpg")
image.flags.writeable = False
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
with mp_hands.Hands(min_detection_confidence=0.5, min_tracking_confidence=0.5) as hands:
  results = hands.process(image)
  image.flags.writeable = True
  image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
  if results.multi_hand_landmarks and results.multi_handedness:
      #print(results.multi_hand_landmarks)
      #print(results.multi_handedness)

      for hand_landmarks,handedness in zip(results.multi_hand_landmarks,results.multi_handedness):
        print("00",hand_landmarks.landmark[1])

        mp_drawing.draw_landmarks(image,hand_landmarks,mp_hands.HAND_CONNECTIONS)
        orgpoint=hand_landmarks.landmark[0]
        x = int(orgpoint.x * image.shape[1])
        y = int(orgpoint.y * image.shape[0])
        cv2.circle(image, (x, y), 10, (255, 0, 0), -1) # 画出关键点

        point=hand_landmarks.landmark[4]
        x = int(point.x * image.shape[1])
        y = int(point.y * image.shape[0])
        cv2.circle(image, (x, y), 5, (255, 0, 0), -1) # 画出关键点
        f=(x-orgpoint.x)/(y-orgpoint.y)
        radians = math.pi / 4  # π/4 弧度，即45度
        degrees = radians * (180 / math.pi)
        cv2.putText(image,str(f) ,(x, y), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 255), 1, cv2.LINE_AA) # 画出关键点

        point=hand_landmarks.landmark[8]
        x = int(point.x * image.shape[1])
        y = int(point.y * image.shape[0])
        cv2.circle(image, (x, y), 10, (255, 0, 0), -1) # 画出关键点

        point=hand_landmarks.landmark[12]
        x = int(point.x * image.shape[1])
        y = int(point.y * image.shape[0])
        cv2.circle(image, (x, y), 10, (255, 0, 0), -1) # 画出关键点

        point=hand_landmarks.landmark[16]
        x = int(point.x * image.shape[1])
        y = int(point.y * image.shape[0])
        cv2.circle(image, (x, y), 10, (255, 0, 0), -1) # 画出关键点

        point=hand_landmarks.landmark[20]
        x = int(point.x * image.shape[1])
        y = int(point.y * image.shape[0])
        cv2.circle(image, (x, y), 10, (255, 0, 0), -1) # 画出关键点

        for point in hand_landmarks.landmark:
                x = int(point.x * image.shape[1])
                y = int(point.y * image.shape[0])
                cv2.circle(image, (x, y), 5, (0, 255, 0), -1) # 画出关键点

cv2.imshow('MediaPipe Hands', cv2.flip(image, 1))
cv2.waitKey()
cv2.destroyAllWindows()